<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 8:43
 */
define('SITE_AUTH', 'user-apply');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Center\EnrollUser as Model;

class userApply extends AdminAction
{
    private $list_url = 'data/modules/user/userApply.php';
    private $status = [
        0 => '等待支付状态',
        1 => '等待发货',
        2 => '等待收获状态',
        3 => '等待绑定体脂秤状态',
        4 => '已绑定体脂秤',
        10 => '锁定失效',
    ];
    private $sex = [
        0 => '未知',
        1 => '男',
        2 => '女',
    ];

    private $order = [
        0 => [
            'name' => '创建时间倒序',
            'order' => 'CreateTime DESC'
        ],
        1 => [
            'name' => '创建时间正序',
            'order' => 'CreateTime ASC'
        ],
        2 => [
            'name' => '报名时间倒序',
            'order' => 'EnrollTime DESC'
        ],
        3 => [
            'name' => '报名时间正序',
            'order' => 'EnrollTime ASC'
        ],
    ];

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['UserId >' => 0];
        $userid = (isset($_GET['s_userid']) and $_GET['s_userid'] !== '') ? trim(intval($_GET['s_userid'])) : null;
        if ($userid !== null) {
            $where += ['UserId' => $userid];
        }
        $name = (isset($_GET['s_name']) and $_GET['s_name'] !== '') ? trim($_GET['s_name']) : null;
        if ($name !== null) {
            $where += ['Name LIKE' => '%' . $name . '%'];
        }
        $mobile = (isset($_GET['s_mobile']) and $_GET['s_mobile'] !== '') ? trim(intval($_GET['s_mobile'])) : null;
        if ($mobile !== null) {
            $where += ['Mobile' => $mobile];
        }
        $sex = (isset($_GET['s_sex']) and $_GET['s_sex'] !== '') ? intval($_GET['s_sex']) : null;
        if ($sex !== null) {
            $where += ['Sex' => $sex];
        }
        $status = (isset($_GET['s_status']) and $_GET['s_status'] !== '') ? intval($_GET['s_status']) : null;
        if ($status !== null) {
            $where += ['Status' => $status];
        }
        $order = intval($_GET['s_order']);
        $s_data = [
            's_userid' => $userid,
            's_name' => $name,
            's_mobile' => $mobile,
            's_sex' => $sex,
            's_status' => $status,
            's_order' => $order,
        ];

        $order_s = $this->order[$order]['order'];

        $page = $_GET['page'] < 1 ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit, '*', $order_s);
        $count = $data['count'];
        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }
        $smarty->value('order', $this->order);
        $smarty->value('status', $this->status);
        $smarty->value('sex', $this->sex);
        $smarty->value('s_data', $s_data);
        $smarty->output('user/userApply_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();
        $id = intval($_GET['id']);
        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $data = Model::getInfoByIds($id);
            //            $data = Model::getItemById($id);
            if (empty($data)) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $smarty->value('info', $data);
        }
        $smarty->output('user/userApply_edit.tpl');
    }

    public function doUpdata()
    {
        $id = intval($_POST['userid']);
        $logisticsNumber = trim($_POST['logisticsNumber']);
        $data = [];
        if (!empty($logisticsNumber)) {
            $data = ['logisticsNumber' => $logisticsNumber, 'Status' => 2];
        } else {
            HDshowMsg('物流单为空！', ['type' => 'error']);
        }
        $rel = Model::update($id, $data);
        if ($rel) {
            HDshowMsg('修改成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('修改失败！', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

}

$app->run();